(function( $, undefined ) {
/*
	\name ru Текстовое поле
	\description ru Текстовое поле, с возможностью autocomplete
	\save true
*/
$.widget("ui.ajajform_textedit",$.ui.ajajform_widget, {
	options: {
		// \type cssclass
		cssclass: "afw-textedit",
		// \type text
		value: "",
		minsize: 0,
		maxsize: undefined,
		// \type boolean
		password: false,
		// \type text
		label: "",
		// \type integer
		width: null,
		readonly: false,
		// \type html
		htmltemplate: '<div class="af-label"></div><div class="af-edit"><input type="text"/><div class="af-error"></div></div><div style="clear: both;"></div>',
		htmltemplate_disableable: '<div class="af-label"></div><div class="af-disable"></div><div class="af-edit"><input type="text"/><div class="af-error"></div></div><div style="clear: both;"></div>',
		// \type any
		// \skip 1
		autocomplete: null,
		// \type function
		validate: null,
		isnumber:false,
		add_decimals:null,
		// \type: function
		change: null
	},
	i18n: {
		en: {
			too_long: "Field is too long",
			too_short: "Field is too short",
			not_number: "Not a number"
		},
		ru: {
			too_long: "Слишком длинное",
			too_short: "Слишком короткое",
			not_number: "Не число"
		}
	},
	_create: function() {
		if (this.options.password) this.genericCreate(function(x) {return x.replace('type="text"','type="password"')}); else this.genericCreate();
		this.refreshLabel();
		this.refreshInput();
		this.refreshValue();
		this.refreshAutocomplete();
		this.refreshChange();
		this.refreshDisabled();
		this.refreshShow_autocomplete();
		this.refreshReadonly();
	},
	refreshPassword: function() {
	},
	refreshShow_autocomplete: function() {
		var t=this;
		$(".af-label .af-autocomplete",this.element).remove();
		if (!this.options.show_autocomplete) return;
		var ac=$("<div class='af-autocomplete'></div>").appendTo($(".af-label",this.element));
		var s=this.options.autocomplete.source;
		for (var i=0;i<s.length;i++) {
			$("<div class='af-ac-item'></div>").text(s[i]).appendTo(ac).bind("click",{d:s[i]},function(e) {t.option("value",e.data.d);});
		}
	},
	refreshAutocomplete: function() {
		var i=$("input",this.element);
		if (this.options.autocomplete) {
			i.autocomplete(this.options.autocomplete);
		} else {
			i.autocomplete("destroy");
		}
	},
	refreshValue: function() {
		var i=$("input",this.element);
		fmtd=this.options.value;
		if (this.options.add_decimals && fmtd!==null && fmtd!==undefined && fmtd.toString().match(/^\d+$/)) fmtd+=this.options.add_decimals;
		i.val(fmtd);
	},
	refreshInput: function() {
		var i=$("input",this.element);
		i.addClass("ui-corner-all af-edit");
		i.width(this.options.width);
		i.bind("focus",function(e) { $(this).addClass("ui-state-highlight"); });
		if (this.options.validate) {
			i.bind("blur",this,function(e) {
				$(this).removeClass("ui-state-highlight");
				e.data.validate();
			});
		} else {
			i.bind("blur",function(e) { $(this).removeClass("ui-state-highlight"); });
		}
	},
	refreshChange: function() {
		var i=$("input",this.element);
		if (!this.options.change) {
			if (this.oldBind) {i.unbind("change",this.oldBind);this.oldBind=null;}
			return;
		}
		if (this.oldBind) i.unbind("change",this.oldBind);
		this.oldBind=this.options.change;
		i.bind("change",{tag:i},this.oldBind);
	},
	refreshDisabled: function() {
		$("input",this.element).attr("disabled",this.options.disabled?true:false);
		this.genericRefreshDisabled();
	},
	refreshReadonly: function() {
		$("input",this.element).attr("readonly",this.options.readonly?true:false);
	},
	destroy: function() {
	},
	validate: function() {
		var vd=this.options.validate;
		var inp=$("input",this.element);
		var val=inp.val();
		var r;
		if (vd) r=vd(val);
		if (!r) {
			if (val.length<this.options.minsize) r=this.gettext("too_short");
			if (this.options.maxsize && val.length>this.options.maxsize) r=this.gettext("too_long");
			if (this.options.isnumber && !(val.match(/^(|\d+\.\d+|\d+\.|\d+\.\d+)$/))) r=this.gettext("not_number");
		}
		if (r) inp.addClass("ui-state-error"); else inp.removeClass("ui-state-error");
		this.refreshError(r);
		return r?true:false;
	},
	postvalue: function(r) {
		r[this.options.name]=$("input",this.element).val();
	}
});

})( jQuery );
